﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class PublicCommunication_UpdateGroupLinkMan : System.Web.UI.Page
{
    private int pcgLm_id;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString.Get("pcglm_id") != null)
            {
                pcgLm_id = int.Parse(Request.QueryString.Get("pcglm_id"));
                this.TxtBirthday.Attributes.Add("onfocus", "calendar()");
                this.FindAllComGroup();
                this.SetTxtContent();
                ViewState["pcgLm_id"] = pcgLm_id;
            }
        }
    }

    /// <summary>
    /// 查找所有通讯组填充列表菜单
    /// </summary>
    protected void FindAllComGroup()
    {
        PublicCommunicationGroupService pcgService = new PublicCommunicationGroupService();
        List<PublicCommunicationGrouptb> list = pcgService.findALL();
        if (list != null && list.Count > 0)
        {
            foreach (PublicCommunicationGrouptb pcg in list)
            {
                this.DdlGroup.Items.Add(new ListItem(pcg.PcgName, pcg.PcgId.ToString()));
            }
        }
    }

    /// <summary>
    /// 查找指定联系人信息填充表单
    /// </summary>
    protected void SetTxtContent()
    {
        if (pcgLm_id != 0)
        {
            PublicCommunicationGroupLinkManService pcgLmService = new PublicCommunicationGroupLinkManService();
            PublicCommunicationGroupLinkMantb pcgLm = pcgLmService.findByCglmId(pcgLm_id);
            if (pcgLm != null)
            {
                foreach (ListItem item in DdlGroup.Items)
                {
                    if (item.Value.Equals(pcgLm.ComGroup.PcgId.ToString()))
                    {
                        item.Selected = true;
                    }
                }
                this.TxtName.Text = pcgLm.GroupLinkManName;
                foreach(ListItem item2 in this.DropDownList2.Items)
                {
                    if(item2.Value.Equals(pcgLm.GroupLinkManSec.ToString()))
                    {
                        item2.Selected = true;
                    }
                }
                this.TxtBirthday.Text = pcgLm.GroupLinkManBirthday.ToShortDateString();
                this.TxtName2.Text = pcgLm.GroupLinkManName2;
                this.TxtDuty.Text = pcgLm.GroupLinkManDuty;
                this.TxtUnitName.Text = pcgLm.GroupLinkManUnitName;
                this.TxtUnitAddress.Text = pcgLm.GroupLinkManUnitAddress;
                this.TxtUnitTelephone.Text = pcgLm.GroupLinkManUnitTelephone;
                this.TxtUnitFacsimile.Text = pcgLm.GroupLinkManUnitFacsimile;
                this.TxtFamilyAddress.Text = pcgLm.GroupLinkManHomeAddress;
                this.TxtFamilyTelephone.Text = pcgLm.GroupLinkManHomeTelephone;
                this.TxtHandset.Text = pcgLm.GroupLinkManHandset;
                this.TxtEmail.Text = pcgLm.GroupLinkManEmail;
                this.TxtOicq.Text = pcgLm.GroupLinkManOicq;
                this.TxtMSN.Text = pcgLm.GroupLinkManMsn;
                this.TxtRemark.Text = pcgLm.GroupLinkManRemark;
            }
        }
    }

    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        if (ViewState["pcgLm_id"] != null)
        {
            pcgLm_id = (int)ViewState["pcgLm_id"];
        }
        string cgLinkManName = Request[this.TxtName.ClientID];
        int comGroupId = int.Parse(Request.Params.GetValues(this.DdlGroup.ClientID)[0]);
        int cgLinkManSec = int.Parse(Request.Params.GetValues(this.DropDownList2.ClientID)[0]);
        DateTime cgLinkManBirthday = new DateTime();
        if (!"".Equals(this.TxtBirthday.Text.Trim()))
        {
            try
            {
                cgLinkManBirthday = DateTime.Parse(Request[this.TxtBirthday.ClientID]);
            }
            catch
            {
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('日期输入有错！');", true);
            }
        }
        string cgLinkManName2 = Request[this.TxtName2.ClientID];
        string cgLinkManDuty = Request[this.TxtDuty.ClientID];
        string cgLinkManUnitName = Request[this.TxtUnitName.ClientID];
        string cgLinkManUnitAddress = Request[this.TxtUnitAddress.ClientID];
        string cgLinkManUnitTelephone = Request[this.TxtUnitTelephone.ClientID];
        string cgLinkManUnitFacsimile = Request[this.TxtUnitFacsimile.ClientID];
        string cgLinkManHomeAddress = Request[this.TxtFamilyAddress.ClientID];
        string cgLinkManHomeTelephone = Request[this.TxtFamilyTelephone.ClientID];
        string cgLinkManHandset = Request[this.TxtHandset.ClientID];
        string cgLinkManEmail = Request[this.TxtEmail.ClientID];
        string cgLinkManQicq = Request[this.TxtOicq.ClientID];
        string cgLinkManMsn = Request[this.TxtMSN.ClientID];
        string cgLinkManRemark = Request[this.TxtRemark.ClientID];
        PublicCommunicationGroupLinkManService pcgLmService = new PublicCommunicationGroupLinkManService();
        if (pcgLmService.updateComGroupLinkManInfoByGroupLinkManId(pcgLm_id, comGroupId, cgLinkManName, cgLinkManSec, cgLinkManBirthday,
            cgLinkManName2, cgLinkManDuty, cgLinkManUnitName, cgLinkManUnitAddress, cgLinkManUnitTelephone, cgLinkManUnitFacsimile,
            cgLinkManHomeAddress, cgLinkManHomeTelephone, cgLinkManHandset, cgLinkManEmail, cgLinkManQicq, cgLinkManMsn, cgLinkManRemark) == 1)
        {
            Response.Redirect("CommunicationGroupLinkManShow.aspx?pcg_id=" + comGroupId);
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('信息更新失败，请重试！');", true);
        }
    }
}
